#include "ServerSocket.h"


ServerSocket :: ServerSocket ( int port ) : Socket ( port ) {
}

ServerSocket :: ~ServerSocket () {
}

void ServerSocket :: abrirConexion () {
	this->serv_addr.sin_addr.s_addr = INADDR_ANY;

	int bindOk = bind ( this->fdSocket,(struct sockaddr *)&(this->serv_addr),sizeof(this->serv_addr) );
	if ( bindOk < 0 )
		throw "Error en bind";

	int listenOk = listen ( this->fdSocket,CONEXIONES_PENDIENTES );
	if ( listenOk < 0 )
		throw "Error en listen";

}

void ServerSocket :: aceptarConexion () {
	struct sockaddr_in cli_addr;

	int longCliente = sizeof ( cli_addr );

	this->nuevoFdSocket = accept ( this->fdSocket,(struct sockaddr *)&cli_addr,(socklen_t *)&longCliente );
	if ( this->nuevoFdSocket < 0 )
		throw "Error en accept";
}

void ServerSocket :: finalizarConexion () {
	close ( this->nuevoFdSocket );
}

int ServerSocket :: enviar ( char* buffer ) {
	int cantBytes = write ( this->nuevoFdSocket,buffer,strlen(buffer) );
	return cantBytes;
}

int ServerSocket :: recibir ( char* buffer,int buffSize ) {
	int cantBytes = read ( this->nuevoFdSocket,buffer,buffSize );
	return cantBytes;
}

void ServerSocket :: cerrarConexion () {
	close ( this->fdSocket );
}

